<?php
class AdminController extends Zendvn_Controller_Action{
	public function init(){
		$this->_arrParam = $this->_request->getParams(); //Mang tham so nhan duoc khi mot Action chay
		$this->_currentController = '/' . $this->_arrParam['module'] . '/' . $this->_arrParam['controller']; //Duong dan cua Controller
		$this->_actionMain = '/' . $this->_arrParam['module'] . '/'	. $this->_arrParam['controller'] . '/index'; //Duong dan cua Action chinh
		
		//Luu cac du lieu filter vaof SESSION
		//Dat ten SESSION
		$this->_namespace = $this->_arrParam['module'] . '-' . $this->_arrParam['controller'];
		$ssFilter = new Zend_Session_Namespace($this->_namespace);
		//Lay thong tin so phan tu tren mot trang
		if(isset($this->_arrParam['limitPage'])){
			$ssFilter->limitPage = $this->_request->getParam('limitPage');
			$this->_paginator['itemCountPerPage'] = $ssFilter->limitPage;
		}elseif(!empty($ssFilter->limitPage)){
			$this->_paginator['itemCountPerPage'] = $ssFilter->limitPage;
		}
		
		//$ssFilter->unsetAll();
		if(empty($ssFilter->col)){
			$ssFilter->id_phongban	= 0;
			$ssFilter->id_nhanvien	= 0;
			$ssFilter->date_nam 	= @date('Y');
		}
		
		$this->_arrParam['ssFilter']['id_phongban'] = $ssFilter->id_phongban;
		$this->_arrParam['ssFilter']['id_nhanvien'] = $ssFilter->id_nhanvien;
		$this->_arrParam['ssFilter']['date_nam'] 	= $ssFilter->date_nam;

		//Truyen ra ngoai view
		$this->view->arrParam = $this->_arrParam;
		$this->view->currentController = $this->_currentController;
		$this->view->actionMain = $this->_actionMain;
	
		$this->view->siteConfig = Zend_Registry::get('siteConfig');
		$template_path = TEMPLATE_PATH . "/admin/" . $this->view->siteConfig['template']['admin'];
		$this->loadTemplate($template_path, 'template.ini', 'template');
	}
	
	public function indexAction(){
		$this->view->Title = 'Phần mềm CRM';
		$this->view->headTitle($this->view->Title, true);
		
		$tblAjax = new Hopdong_Model_Ajax();
		$this->view->slbPhongban 	= $tblAjax->itemInSelectbox($this->_arrParam, array('task' => 'get-phongban'));
		$this->view->slbNhanvien 	= $tblAjax->itemInSelectbox(array('id_phongban' => $this->_arrParam['ssFilter']['id_phongban']), array('task' => 'get-nhanvien'));
		$this->view->Phongban 		= $tblAjax->getItem(array('id_phongban' => $this->_arrParam['ssFilter']['id_phongban']), array('task' => 'get-phongban'));
		$this->view->Nhanvien 		= $tblAjax->getItem(array('id_nhanvien' => $this->_arrParam['ssFilter']['id_nhanvien']), array('task' => 'get-nhanvien'));
		$this->view->Items 			= $tblAjax->bieudo($this->_arrParam, array('task'=>'admin-list'));
		
		$tblMarketing = new Marketing_Model_Ajax();
		$this->view->Marketing		 = $tblMarketing->listItem($this->_arrParam, array('task' => 'admin-list'));
		
		$tblKhachhang = new Khachhang_Model_Ajax();
		$this->view->Khachhang		 = $tblKhachhang->listItem($this->_arrParam, array('task' => 'admin-list'));
		
		$tblHopdong = new Hopdong_Model_Ajax();
		$this->view->Hopdong		 = $tblHopdong->listItem($this->_arrParam, array('task' => 'admin-list'));
	}
	
	public function filterAction() {
		$this->_helper->viewRenderer->setNoRender();
		$this->_helper->layout->disableLayout();
		$ssFilter = new Zend_Session_Namespace($this->_namespace);
	
		if ($this->_arrParam['type'] == 'id_phongban') {
			$ssFilter->id_phongban = $this->_arrParam['id_phongban'];
			$ssFilter->id_nhanvien = 0;
		}
	
		if ($this->_arrParam['type'] == 'id_nhanvien') {
			$ssFilter->id_nhanvien = $this->_arrParam['id_nhanvien'];
		}
	
		if ($this->_arrParam['type'] == 'date_nam') {
			$ssFilter->date_nam = $this->_arrParam['date_nam'];
		}
	
		$this->_redirect($this->_actionMain);
	}
	
	public function countAction(){
		$this->_helper->layout->disableLayout();
	}
}